package com.easychat.controller;

import com.easychat.annotation.GlobalInterceptor;
import com.easychat.entity.constants.Constants;
import com.easychat.entity.dto.SysSettingDto;
import com.easychat.entity.dto.TokenUserInfoDto;
import com.easychat.entity.vo.ResponseVO;
import com.easychat.entity.vo.SysSettingVO;
import com.easychat.entity.vo.UserInfoVO;
import com.easychat.exception.BusinessException;
import com.easychat.redis.RedisComponet;
import com.easychat.redis.RedisUtils;
import com.easychat.service.UserInfoService;
import com.easychat.utils.CopyTools;
import com.wf.captcha.ArithmeticCaptcha;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.Pattern;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @program: easychat-java
 * @description:
 * @author: Mr.Wang
 * @create: 2024-11-17 15:31
 **/

@RestController
@RequestMapping("/account")
@Validated
public class AccountController extends ABaseController {


    @Resource
    private RedisUtils redisUtils;

    @Resource
    private UserInfoService userInfoService;

    @Resource
    private RedisComponet redisComponet;

    /**
     * 获取验证码
     *
     * @return
     */
    @PostMapping("/checkCode")
    public ResponseVO checkCode() {
        ArithmeticCaptcha captcha = new ArithmeticCaptcha(100, 42);
//        验证码结果 text
        String text = captcha.text();
//        验证码图片
        String base64 = captcha.toBase64();
//        生成UUID作为验证码id
        String checkCodeKey = UUID.randomUUID().toString();
//        将验证码id和对应答案存入redis 超时时间十分种
        redisUtils.setex(Constants.REDIS_KEY_CHECK_CODE + checkCodeKey, text, 60 * 10);

        Map<String, String> result = new HashMap<>();
//        封装验证码图片返回
        result.put("checkCode", base64);
//        返回验证码对应id 方便后续登录校验对应的验证码答案
        result.put("checkCodeKey", checkCodeKey);
        return getSuccessResponseVO(result);
    }

    /**
     * 登录
     *
     * @return
     */
    @RequestMapping("/register")
    public ResponseVO register(@NotEmpty String checkCodeKey,
                               @NotEmpty String checkCode,
                               @NotEmpty @Email String email,
                               @NotEmpty String nickName,
                               @NotEmpty String password) {

        try {

            if (!checkCode.equalsIgnoreCase((String) redisUtils.get(Constants.REDIS_KEY_CHECK_CODE + checkCodeKey))){
                    throw new BusinessException("验证码错误");
                }
            userInfoService.register(email, nickName, password);
            return getSuccessResponseVO(null);
            }finally {
            redisUtils.delete(Constants.REDIS_KEY_CHECK_CODE + checkCodeKey);
        }

    }

    /**
     * 登录
     * @param checkCodeKey
     * @param checkCode
     * @param email
     * @param password
     * @return
     */
    @RequestMapping("/login")
    public ResponseVO login(@NotEmpty String checkCodeKey,
                            @NotEmpty String checkCode,
                            @NotEmpty @Email String email,
                            @NotEmpty String password) {

        try {
            if (!checkCode.equals(redisUtils.get(Constants.REDIS_KEY_CHECK_CODE + checkCodeKey))){
                throw new BusinessException("验证码错误");
            }
            UserInfoVO userInfoVO = userInfoService.login(email, password);
            return getSuccessResponseVO(userInfoVO);
        }finally {
            redisUtils.delete(Constants.REDIS_KEY_CHECK_CODE + checkCodeKey);
        }

    }

    @RequestMapping(value = "/getSysSetting")
    @GlobalInterceptor
    public ResponseVO getSysSetting() {
        SysSettingDto sysSettingDto = redisComponet.getSysSetting();
        return getSuccessResponseVO(CopyTools.copy(sysSettingDto, SysSettingVO.class));
    }


}
